<?php
class PantsPage extends PantsObject {
  public $id;
  public $title;
  public $name;
  public $content;
  public $posted;
  public $modified;
  
  private $content_rendered;

  // -- Public Static Methods -------------------------------------------------
  public static function createFromResponse() {
    if ($id = Pants::getResponse('pageId')) {
      $page = PantsPage::getById($id);
    }
    else {
      $page = new PantsPage();
    }

    // Name.
    $page->name = strtolower(trim(Pants::getResponse('pageName', $page->name)));
    $page->name = preg_replace('/\s+/', '-', $page->name);
    $page->name = preg_replace('/[^0-9A-Za-z_-]/', '', $page->name);
    
    if ($page->id == 0 &&
        Pants::query('pages.getByName', array('name' => $page->name))) {
      Pants::error('Duplicate Page Name',
          'There is already a page with the name "'.$page->name.'".');
    }
    
    // Title.
    $page->title = trim(Pants::getResponse('pageTitle', $page->title));
    $page->title = str_replace(array('<', '>'), array('&lt;', '&gt;'),
        $page->title);
    
    // Content.
    $page->content = trim(Pants::getResponse('pageContent', $page->content));

    return $page;
  }

  public static function getById($id) {
    $page = Pants::query('pages.getById', array('id' => $id));

    if (!$page) {
      throw new PantsException('Invalid page id.');
    }

    return $page;
  }

  public static function getByName($name) {
    $page = Pants::query('pages.getByName', array('name' => $name));

    if (!$page) {
      throw new PantsException('Invalid page name.');
    }

    return $page;
  }

  public static function getCount() {
    $result = Pants::query('pages.getCount');

    if ($result) {
      $result = $result->fetch_array();
      return $result[0];
    }

    return 0;
  }

  public static function load($row) {
    return new PantsPage($row['id'], $row['title'], $row['name'],
        $row['content'], $row['posted'], $row['modified']);
  }

  public static function search($query) {
    return Pants::query('pages.search', array('query' => $query));
  }

  // -- Public Instance Methods ------------------------------------------------
  public function __construct($id = 0, $title = '', $name = '', $content = '',
      $posted = 0, $modified = 0) {

    $this->id       = $id;
    $this->title    = $title;
    $this->name     = $name;
    $this->content  = $content;
    $this->posted   = $posted ? $posted : time();
    $this->modified = $modified ? $modified : time();
  }

  public function getContent() {
    if (!$this->content_rendered) {
      $this->content_rendered = $this->render();
    }
    
    return $this->content_rendered;
  }
  
  public function save() {
    if ($this->id == 0) {
      $result = Pants::query('pages.create', array(
        'name'    => $this->name,
        'title'   => $this->title,
        'content' => $this->content
      ));

      if ($result) {
        $this->id = Pants::$db->getInsertId();
      }
    }
    else {
      $result = Pants::query('pages.update', array(
        'id'      => $this->id,
        'name'    => $this->name,
        'title'   => $this->title,
        'content' => $this->content
      ));
    }

    return $result;
  }
  
  // -- Private Instance Methods -----------------------------------------------
  
  private function render() {
    if (strpos($this->content, '<?php')) {
      ob_start();
      eval('?>'.$this->content);
      $content = ob_get_contents();
      ob_end_clean();
    }
    else {
      $content = $this->content;
    }

    // Parse wiki-style links to other pages.
    $content = preg_replace('/\[\[([0-9A-Za-z_-]+)\|(.+?)\]\]/',
        '<a href="'.SITE_URL.'/page/$1">$2</a>', $content);
    $content = preg_replace('/\[\[([0-9A-Za-z_-]+)\]\]/',
        '<a href="'.SITE_URL.'/page/$1">$1</a>', $content);

    // Parse wiki-style links to articles.
    $content = preg_replace('/\[\[@(\d+)\|(.+?)\]\]/',
        '<a href="'.SITE_URL.'/article/$1">$2</a>', $content);
    $content = preg_replace('/\[\[@(\d+)\]\]/',
        '<a href="'.SITE_URL.'/article/$1">$1</a>', $content);

    return $content;
  }
}
?>